<?php
/**
 * Created by PhpStorm.
 * User: DaibiDaddy
 * Date: 2016/5/24
 * Time: 17:14
 */

namespace App\Base;
use Symfony\Component\Form\Extension\Core\Type\FormType;
use Symfony\Component\Form\Extension\HttpFoundation\HttpFoundationExtension;
use Symfony\Component\Form\Forms;
use Symfony\Component\HttpFoundation\Request;

class FormBuilder extends BaseController
{

    public $method = "POST";
    public $labelPosition = "left";

    /**
     * @param $entity
     * @param $formConf
     * @param $actionUrl
     * @return \Symfony\Component\Form\FormInterface
     */
    public function createAddFrom($entity,$formConf,$actionUrl)
    {
        $this->toArray($entity);
        $options = array(
            "attr"  => array(
                "action" => $actionUrl,
                "method" => $this->method,
                "labelPosition" => $this->labelPosition,
                "en"    => str_replace("\/","/",json_encode($entity)),
            ),
        );
        $formFactory = Forms::createFormFactoryBuilder()
            ->addExtension(new HttpFoundationExtension())
            ->getFormFactory()->createBuilder(FormType::class, $entity, $options);
        foreach ($formConf as $key => $param) {
            $formFactory->add(
                $key,
                $param->getType(),
                $param->getData()
            );
        }
        $form = $formFactory->getForm();
//        $show = $form->createView();
        return $form;
    }

    public function fillEntity($entity,$request,$formConf)
    {
        $formFactory = Forms::createFormFactoryBuilder()
            ->addExtension(new HttpFoundationExtension())
            ->getFormFactory()->createBuilder(FormType::class, $entity);

        foreach ($formConf as $key => $param) {
            $formFactory->add(
                $key
            );
        }

        $form = $formFactory->getForm();
        $form->handleRequest($request);
        return $form->getData();
    }
//
//    /**
//     * @param $actionUrl
//     * @param $entity
//     * @return \Symfony\Component\Form\FormBuilderInterface
//     */
//    public function createNoneEntityForm($actionUrl,$entity)
//    {
//        $options = array(
//            "attr"  => array(
//                "action" => $actionUrl,
//                "method" => $this->method,
//                "labelPosition" => $this->labelPosition,
//                "en"    => str_replace("\/","/",json_encode($entity)),
//            ),
//        );
//        $formFactory = Forms::createFormFactoryBuilder()
//            ->addExtension(new HttpFoundationExtension())
//            ->getFormFactory()->createBuilder(FormType::class, $entity, $options);
//        return $formFactory;
//    }


    public function simpleForm($actionUrl,$rows)
    {
        $entity = [];
        foreach ($rows as $one) {
            $entity[$one->getName()] = "";
        }
        $options = array(
            "attr"  => array(
                "action" => $actionUrl,
                "method" => $this->method,
                "labelPosition" => $this->labelPosition,
                "en"    => str_replace("\/","/",json_encode($entity)),
            ),
        );
        $formFactory = Forms::createFormFactoryBuilder()
            ->addExtension(new HttpFoundationExtension())
            ->getFormFactory()->createBuilder(FormType::class, $entity, $options);
        foreach ($rows as $one) {
            $formFactory->add($one->getName(),$one->getType(),$one->getData());
        }
        return $formFactory->getForm();

    }

    public function setLabelLTop()
    {
        $this->labelPosition = "top";
        return $this;
    }



    public function showExample()
    {
//        $uid = $this->needToBeAdmin();
//        $action = $this->generateUrl("CarFuelLogDoCheck",array("id"=>$entity->getId()));
//        $SiteSetting = new SiteSetting($this->getDoctrine());
//        $fc = new FromCreator();
//        $show = $fc->createAddFrom($entity,$SiteSetting->formConfigStatus(),$action);
//        $data = array(
//            "title" => "加油审核",
//            "show"   => $show,
//        );
//        return $this->render("Base/common.add.html.twig",$data);
    }
    public function saveExample()
    {
//        $uid = $this->needToBeAdmin();
//        $CarFuelLogSetting = new CarFuelLogSetting($this->getDoctrine());
//        $fc = new FromCreator();
//        $fc->fillEntity($entity,$request,$CarFuelLogSetting->formConfigCheck());
//        $this->myEm()->persist($entity);
//        $this->myEm()->flush();
//        return $this->js_out("保存成功",$this->generateUrl("CarFuelLogLists"));
    }
}
